//
//  CALMainViewController.m
//  CalCounter
//
//  Created by Le thanh Lam on 06/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALMainViewController.h"
#import "CALMealsViewController.h"
#import "CALWeightViewController.h"
#import "CALMeasurementViewController.h"
#import "CustomTableViewCell1.h"
#import "DateTimeView.h"
#import "DataHelper.h"
#import "NSDate+Ex.h"
#import "CALDiaryInfo.h"
#import "MainCell.h"

@interface CALMainViewController ()<DateTimeViewDelegate>{
    
    __weak IBOutlet DateTimeView *dateBarView;
    NSString *strCals;
    NSString *strWeight;
}
@end

@implementation CALMainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    UIView *titleView = [[NSBundle mainBundle] loadNibNamed:@"AppTitleView" owner:nil options:nil][0];
    
    self.navigationItem.titleView = titleView;
//    [self.navigationItem setTitle:NSLocalizedString(@"calcounter", nil)];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [dateBarView refresh];
    strCals = [NSString stringWithFormat:@"%1.1f Cals",[[DataHelper sharedDatabase] getCalsInDate:[[DateTimeView selectDate] dateString]]];
    CALDiaryInfo *info = [[DataHelper sharedDatabase] getDiaryInfoByDate:[DateTimeView selectDate]];
    strWeight = [NSString stringWithFormat:@"%1.1f kg",info.diaryWeight];
    [tableViewMain reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DateTimeViewDelegate
- (void)whenChangeTime:(NSDate *)_date{
    strCals = [NSString stringWithFormat:@"%1.1f %@",[[DataHelper sharedDatabase] getCalsInDate:[[DateTimeView selectDate] dateString]], NSLocalizedString(@"cals", nil)];
    CALDiaryInfo *info = [[DataHelper sharedDatabase] getDiaryInfoByDate:[DateTimeView selectDate]];
    strWeight = [NSString stringWithFormat:@"%1.1f kg",info.diaryWeight];
    [tableViewMain reloadData];
}
#pragma mark - tableView 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 61;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    MainCell *cell = (MainCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = (MainCell *)[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil][0];
    }
    switch (indexPath.row) {
        case 0:{
            cell.img.image = [UIImage imageNamed:@"icon_meals.png"];
            cell.lblSubject.text = NSLocalizedString(@"meals", nil);
            cell.lblResult.text = strCals;
            break;
        }
        case 1:{
            cell.img.image = [UIImage imageNamed:@"icon_weight.png"];
            cell.lblSubject.text = NSLocalizedString(@"weight", nil);
            cell.lblResult.text = strWeight;
            break;
        }
        case 2:{
            cell.img.image = [UIImage imageNamed:@"icon_measurements.png"];
            cell.lblSubject.text = NSLocalizedString(@"measurement", nil);
            cell.lblResult.text =@"";
            break;
        }
        default:
            break;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:{
            CALMealsViewController *viewController = [[CALMealsViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        }
        case 1:{
            CALWeightViewController *viewController = [[CALWeightViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        }
        case 2:{
            CALMeasurementViewController *viewController = [[CALMeasurementViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            break;
        }
        default:
            break;
    }

}


@end







